package com.ddc.study.base;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.ddc.study.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

/**
 * Created on 2018/4/19.
 *
 * @author lshun
 * @version 1.0
 */
public class MyDialogFragment extends DialogFragment {
    public static DialogFragment getInstance(@StringRes int resId) {
        MyDialogFragment dialogFragment = new MyDialogFragment();
        dialogFragment.mMsg = dialogFragment.getString(resId);
        return dialogFragment;
    }

    public static MyDialogFragment getInstance(String msg) {
        MyDialogFragment dialogFragment = new MyDialogFragment();
        dialogFragment.mMsg = msg;
        return dialogFragment;
    }

    public static MyDialogFragment getInstance(Dialog dialog) {
        MyDialogFragment dialogFragment = new MyDialogFragment();
        dialogFragment.mDialog = dialog;
        return dialogFragment;
    }

    private String mMsg;
    private Dialog mDialog;

    public void setMsg(String msg) {
        mMsg = msg;
    }

    public void setDialog(Dialog dialog) {
        mDialog = dialog;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        if (mDialog != null) return mDialog;

        View v = View.inflate(getActivity(), R.layout.layout_progress_dialog, null);
        TextView tv = v.findViewById(R.id.message);
        tv.setText(mMsg);

        if (getActivity() == null) return super.onCreateDialog(savedInstanceState);

        return new AlertDialog.Builder(getActivity())
                .setTitle(null)
                .setView(v)
                .create();
    }
}
